// pages/bind/bind.js
let url = getApp().url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    inputValue: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    const app = getApp();
    console.log(app.globalData.userId);

    wx.request({
      url: url + '/getUserDynamic',
      method:'GET',
      data:{userId : app.globalData.userId},
      success:function(res){
        var list = res.data;
        if(list==null){
          var toastText='获取动态失败';
          wx.showToast({
            title: toastText,
            icon:'',
            duration:2000 //弹出时间
          })
        }
        else{
          that.setData({
            list : list
          });
        }
      }
    })
  },

  addDynamic : function(e) {
    var that=this;

    wx.request({
      url: url + "/addUserDynamic",
      method:"post",
      header:{
        "Content-Type": "application/x-www-form-urlencoded" 
      },
      data:{
        userId : getApp().globalData.userId,
        dynamic : e.detail.value.dynamic
      },
      success:function(res){
        console.log(res);
        // wx.reLaunch({
        //   url: "../personalDynamic/personalDynamic"
        // })
        that.setData({
          'inputValue': ''
        });
        that.onLoad()
      }
    })
  },

  deleteDynamic: function (e) {
    var that=this;
    console.log("ID" + e.target.dataset.areaid);
    wx.showModal({
      title: '提示',
      content: '确定要删除该动态吗？',
      success:function(sm){
        if(sm.confirm){
          wx.request({
            url: url + '/deleteUserDynamicByUserDynamicId',
            data: { userDynamicId : e.target.dataset.areaid},
            method:'GET',
            success:function(res){
              var toastText="删除成功";
              
              that.data.list.splice(e.target.dataset.index,1);
              that.setData({
                list:that.data.list
              });
              
              wx.showToast({
                title: toastText,
                icon:'',
                duration:2000
              });
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})